﻿$(() => {
    getReportStatusText = (reportStatus,isArchive) => {
        const reportStatusExpression = `isArchive?'归档':value=='InitialWriting'?'初写中':value=='InitialWritten'?'已初写':value=='Reporting'?'报告中':value=='Reported'?'未审核':value=='Auditing'?'审核中':value=='Audited'?'待复审':value=='Reviewing'?'复审中':value=='Reviewed'?'已复审':value=='Completed'?'已完成':'未报告'`;
        return exprEval.Parser.evaluate(reportStatusExpression, { value: reportStatus ?? null, isArchive });
    }

    getReportStatusColor = reportStatus => {
        switch (reportStatus) {
            case 'InitialWriting':
            case 'Reporting':
            case 'Auditing':
            case 'Reviewing':
                return '#8e44adca';
            case 'InitialWritten':
            case 'Reported':
            case 'Audited':
                return '#d35400ca';
            case 'Reviewed':
            case 'Completed':
                return '#2980b9ca';
            default:
                return '#c0392bca';
        }
    };

    createReportStatusBadge = (reportStatus, isArchive) => {
        const text = getReportStatusText(reportStatus, isArchive);
        const color = getReportStatusColor(reportStatus);
        return $('<span class="badge rounded-pill" />')
            .css('cssText', `font-size:12px;font-weight:normal;background-color:${color} !important`)
            .text(text);
    };

    getQueuingStatusText = queuingStatus => {
        const queuingStatusExpression = `value=='Check'?'检查':value=='Emergency'?'急诊':value=='Ready'?'准备':value=='Paused'?'暂缓':value=='Referral'?'转诊':value=='Skip'?'过号':value=='ReturnVisit'?'复诊':value=='Wait'?'等候':value=='Continued'?'待续':value=='Completed'?'完成':value=='Pending'?'挂起':value=='GiveUp'?'放弃':''`;
        return exprEval.Parser.evaluate(queuingStatusExpression, { value: queuingStatus ?? null });
    }

    getQueuingStatusColor = queuingStatus => {
        switch (queuingStatus) {
            case 'Paused':
            case 'Referral':
            case 'Skip':
            case 'ReturnVisit':
            case 'Continued':
            case 'Pending':
            case 'GiveUp':
                return '#2c3e50ca';
            case 'Emergency':
                return '#c0392bca';
            case 'Ready':
            case 'Check':
                return '#8e44adca';
            case 'Wait':
                return '#d35400ca';
            case 'Completed':
                return '#2980b9ca';
            default:
                return '#2c3e50ca';
        }
    };

    createQueuingStatusBadge = queuingStatus => {
        const text = getQueuingStatusText(queuingStatus);
        const color = getQueuingStatusColor(queuingStatus);
        return $('<span class="badge rounded-pill" />')
            .css('cssText', `font-size:12px;font-weight:normal;background-color:${color} !important`)
            .text(text);
    };

    createExamCancellationBadge = text => {
        return $('<span class="badge rounded-pill" />')
            .css('cssText', `font-size:12px;font-weight:normal;background-color: #c0392bca !important`)
            .text(text ?? '取消');
    };

    createEmergencyBadge = isEmergency => {
        if (isEmergency) {
            return $('<span class="badge rounded-pill" />')
                .css('cssText', `font-size:12px;font-weight:normal;background-color: #c0392bca !important`)
                .text('急诊');
        } else {
            return $('<span />');
        }
    };

    createPatientBadge = patient => {
        const $container = $('<div />').css('cssText', `position:absolute;display:flex;right:2px;top:2px;font-size:12px;font-weight:normal`);

        if (patient.imageZipStatus === 'Completed') {
            $('<img class="dx-icon" alt="" src="/icons/antd_cloud_download.svg">')
                .css('cssText', `width:20px !important;opacity:0.4`)
                .attr('title', '图像已打包完成，现可直接导出图像')
                .appendTo($container);
        }

        if (patient.isConsultation) {
            $('<span class="badge rounded-pill" />')
                .css('cssText', `padding:2px 0 0 2px;line-height:10px;width:17px;height:17px;border:1px solid #3498db99;color:#3498db99;background-color:none !important`)
                .attr('title', patient.expertGroup)
                .text('专')
                .appendTo($container);
        }

        if (patient.patientFeaturesText) {
            $('<span class="badge rounded-pill" />')
                .css('cssText', `padding:2px 0 0 2px;margin-left:2px;width:17px;height:17px;border:1px solid #9b59b699;color:#9b59b699;background-color:none !important`)
                .attr('title', patient.patientFeaturesText)
                .text('特')
                .appendTo($container);
        }

        return $container;
    };

    createPatientTypeSign = (outPatientNumber, inPatientNumber, physicalExamNumber) => {
        const color = inPatientNumber ? '#c0392b80' : physicalExamNumber ? '#2980b980' : '';
        if (color && !outPatientNumber) {
            return $('<div />')
                .css('position', 'absolute')
                .css('right', '0')
                .css('bottom', '0')
                .css('width', '7px')
                .css('height', '7px')
                .css('transform', 'rotate(180deg)')
                .css('background-color', color)
                .css('clip-path', 'polygon(0 0,100% 0,0 100%,0 0)');
        };
    };

    window.examGrid = {
        getReportStatusText,
        getReportStatusColor,
        createReportStatusBadge,
        getQueuingStatusText,
        getQueuingStatusColor,
        createQueuingStatusBadge,
        createEmergencyBadge
    };

    $.fn.examGrid = function (options) {
        return $(this[0]).dataGrid({
            ...options,
            onRowPrepared(e) {
                if (e.rowType === 'data' && e.data) {
                    if (e.data.isEmergency) {
                        e.rowElement.css('background-color', '#c0392b16');
                    }
                    if (e.data.examStatus === 'Cancellation') {
                        e.rowElement.css('text-decoration-line', 'line-through');
                    }
                }
            },
            onCellPrepared(e) {
                if (e.rowType === 'data' && e.data) {
                    if (e.column.dataField === 'accessionNumber') {
                        e.cellElement.css('position', 'relative').append(createPatientTypeSign(e.data.outPatientNumber, e.data.inPatientNumber, e.data.physicalExamNumber));
                    } else if (e.column.dataField === 'reportStatus') {
                        if (e.data.examStatus === 'Cancellation') {
                            e.cellElement.empty().append(createExamCancellationBadge('被取消'));
                        } else {
                            e.cellElement.empty().append(createReportStatusBadge(e.data.reportStatus, e.data.isArchive));
                        }
                    } else if (e.column.dataField === 'queueStatus') {
                        if (e.data.examStatus === 'Cancellation') {
                            e.cellElement.empty().append(createExamCancellationBadge('取消'));
                        } else {
                            e.cellElement.empty().append(createQueuingStatusBadge(e.data.queueStatus));
                        }
                    } else if (e.column.dataField === 'isEmergency') {
                        e.cellElement.empty().append(createEmergencyBadge(e.data.isEmergency));
                    } else if (e.column.dataField === 'patientName') {
                        e.cellElement.css('position', 'relative').append(createPatientBadge(e.data));
                    }
                }
            },
        });
    }
});